<?php

/**
 * @author Grover Campos
 * @version 1.0
 * @created 03-ago-2009 06:43:08 p.m.
 */
class Gtux_Model_Variable extends Gtux_Model_Abstract 
{
  /**
   * @var int
   */
	private $_id_variable;
	/**
	 * @var string
	 */
	private $_nombre;
	/**
	 * @var string
	 */
	private $_valor;

  const CONFIG_BASEDATOS = 1;
  const CONFIG_CLAVE     = 2;
  const CONFIG_USUARIO   = 3;
  const CONFIG_HOST      = 4;
  
	/**
	 * Devuelve el id de la variable
	 *
	 * @return int
	 */
	public function getIdVariable()
	{
		return $this->_id_variable;
	}

	/**
	 * Establece el id de la variable
	 * 
	 * @param int $id_variable
	 */
	public function setIdVariable($id_variable)
	{
	  if (Zend_Validate::is($id_variable, 'Int'))
	  {
		  $this->_id_variable = $id_variable;
	  }
	}

	/**
	 * Devuelve el nombre
	 *
	 * @return string
	 */
	public function getNombre()
	{
		return $this->_nombre;
	}

	/**
	 * Establece el nombre
	 * 
	 * @param string $nombre
	 */
	public function setNombre($nombre)
	{
		$this->_nombre = trim($nombre);
	}

	/**
	 * Devuelve el valor
	 *
	 * @return string
	 */
	public function getValor()
	{
		return $this->_valor;
	}

	/**
	 * Establece el valor
	 * 
	 * @param string $valor
	 */
	public function setValor($valor)
	{
		$this->_valor = trim($valor);
	}

  /**
   * @see Gtux_Model_Abstract::getGateway()
   *
   * @return Gtux_Model_Gateway_Abstract
   */
  public function getGateway()
  {
    if (null == $this->_gateway) 
    {
      $this->setGateway(new Gtux_Model_Gateway_Variable());
    }
    return $this->_gateway;
  }

  /**
   * Devuelve el formulario de variables
   *
   * @return Zend_Dojo_Form
   */
  public function getFormulario(array $valores = null) 
  {
    $form = new Zend_Dojo_Form();
    $form->setAction('/setup/configurar')
         ->setAttrib('id', 'frmVariables')
         ->setMethod('post');
    
    $form->setDecorators(array(
        'FormElements',
        array('HtmlTag', array('tag' => 'table')),
        'DijitForm'
    ));
        
    $elements = new ArrayObject();
    // host
    $variable = $valores[self::CONFIG_HOST];
    $element = new Zend_Dojo_Form_Element_TextBox('host');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtHost')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Host')
            ->setRequired(false)
            ->setOrder(1)
            ->setTrim(true)
            ->setMaxLength(255)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }                                
    $elements[] = $element;
    
    // user
    $variable = $valores[self::CONFIG_USUARIO];
    $element = new Zend_Dojo_Form_Element_TextBox('user');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtUser')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Usuario')
            ->setRequired(false)
            ->setOrder(2)
            ->setTrim(TRUE)
            ->setMaxLength(50)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }
    $elements[] = $element;

    // password
    $variable = $valores[self::CONFIG_CLAVE];
    $element = new Zend_Dojo_Form_Element_PasswordTextBox('clave');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtClave')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Clave')
            ->setRequired(false)
            ->setOrder(3)
            ->setTrim(TRUE)
            ->setMaxLength(50)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }
    $elements[] = $element;
    
    // base de datos
    $variable = $valores[self::CONFIG_BASEDATOS];
    $element = new Zend_Dojo_Form_Element_TextBox('base_datos');
    $element->setAllowEmpty(false)
            ->setAttrib('id', 'txtBaseDatos')
            ->setAutoInsertNotEmptyValidator(TRUE)
            ->setLabel('Base de datos')
            ->setRequired(false)
            ->setOrder(4)
            ->setTrim(TRUE)
            ->setMaxLength(50)
            ->addValidator(new Zend_Validate_StringLength(4));
    if ($variable instanceof Gtux_Model_Variable) 
    {
      $element->setValue($variable->getValor());
    }
    $elements[] = $element;
    
    // editar
    $element = new Zend_Form_Element_Hidden('editar');
    $element->setAttrib('id', 'hdnEditar')
            ->setValue('1');
    $elements[] = $element;            

    // creo el boton de submit
    $submit = new Zend_Dojo_Form_Element_SubmitButton('enviar');
    $submit->setValue('Enviar')
           ->setAttrib('id', 'smbEnviar')
           ->setOrder(5)
           ->setDecorators(array(
             'DijitElement',
             array('decorator' => array('td' => 'HtmlTag'), 'options' => array('tag' => 'td', 'colspan' => 2, 'style' => 'text-align: center;')),
             array('decorator' => array('tr' => 'HtmlTag'), 'options' => array('tag' => 'tr'))
           ));       
    
    foreach ($elements as $item) 
    {
      $item->setDecorators(array(
        'DijitElement',
        'Description',
        'Errors',
        array('decorator' => array('td' => 'HtmlTag'), 'options' => array('tag' => 'td')),
        array('Label', array('tag' => 'td')),
        array('decorator' => array('tr' => 'HtmlTag'), 'options' => array('tag' => 'tr')),
      ));
      $form->addElement($item);
    }
    $form->addElement($submit);
    
    return $form;         
  } 
  
  /**
   * Devuelve la conexión a la base de datos
   *
   * @return Zend_Db_Adapter_Mysqli
   */
  public function getDbConn() 
  {
    $host    = '';
    $usuario = '';
    $clave   = '';
    $bd      = '';
    $ns = new Zend_Session_Namespace(Zend_Registry::get('ns'));
    
    $variable = $ns->variables[Gtux_Model_Variable::CONFIG_HOST];
    if ($variable instanceof self)
    {
      $host     = $variable->getValor();
    }
    
    $variable = $ns->variables[Gtux_Model_Variable::CONFIG_USUARIO];
    if ($variable instanceof self)
    {
      $usuario  = $variable->getValor();
    }
    
    $variable = $ns->variables[Gtux_Model_Variable::CONFIG_CLAVE];
    if ($variable instanceof self)
    {
      $clave    = $variable->getValor();
    }
    
    $variable = $ns->variables[Gtux_Model_Variable::CONFIG_BASEDATOS];
    if ($variable instanceof self)
    {
      $bd       = $variable->getValor();
    }
        
    $params = array(
      'host' => $host,
      'username' => $usuario,
      'password' => $clave,
      'dbname'   => $bd
    );
    
    $fb         = new Zend_Db();
    $base_datos = $fb->factory('Mysqli', $params);
    $profiler = new Zend_Db_Profiler_Firebug('DbConn '.$bd.' Queries');
    $profiler->setEnabled(true);
    $base_datos->setProfiler($profiler);
    $ns->BaseDatos = $base_datos;
    return $base_datos;
  }
  
  /**
   * 
   * @return array
   */
  public function __sleep() 
  {
  	return array('_id_variable', '_nombre', '_valor');
  }
  
}
?>